import numpy as np
import cv2
import time,datetime

from flask import app

from app.kernal.Camera import camera
#import app.kernal.Camera

class Vedio:
    def __init__(self):
        self.__url = "default"
        self.__starttime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

        # dingshiqi here

    def startdireact(self):
        #开始直播
        print("cameraing")
        nowtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
        #url =
        url = './static/vedio/direact/direact102.avi'
        #creact vedio file here
        camera.addurl(url)
        print(nowtime)
        return url,nowtime

    def endireact(self,url='./static/vedio/direact/direact102.avi'):
        #结束直播
        camera.delurl(url)
        print("over dre")
        return True

    def startmonitor(self):
        #开启或重启机器时候调用，重新生成本地监控文件
        nowtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
        self.__starttime = nowtime
        #url = 'monitor' + nowtime + ".avi"
        url = './static/vedio/monitor/veo'+self.__starttime+'.mp4'
        self.__url = url

        #调用camera的开始函数重新生成文件
        starttime = camera.startcamera(url)
        #放到数据库里
        return url

    def endmonitor(self):
        #关闭录像，用于定时生成视频文件
        url = self.__url
        flag = camera.delurl(url)
        print("over mon")
        return flag


'''

cap = cv2.VideoCapture("output.avi")


while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        #just turn the picture
        #frame = cv2.flip(frame,0)

        #if test acp delet the window here and test again
        cv2.imshow('frame',frame)
        key = cv2.waitKey(25)
        if key == 27:
            break
    else:
        break
'''
# import numpy as np
# import cv2
# import time,datetime
# from app.kernal.Camera import camera
#
# class Vedio:
#     def __init__(self):
#         self.__url,self.__starttime = camera.start()
#         # dingshiqi here
#
#     def startdireact(self):
#         #开始直播
#         nowtime = time.asctime( time.localtime(time.time()) )
#         url = ''
#         #creact vedio file here
#
#         return url,nowtime
#
#     def endireact(self):
#         #结束直播
#         return True
#
#     def startmonitor(self):
#         #开始录像，用于启动机器
#         return self.__url
#     def endmonitor(self):
#         #关闭录像，用于定时生成视频文件
#         return True
# '''
#
# cap = cv2.VideoCapture("output.avi")
#
#
# while(cap.isOpened()):
#     ret, frame = cap.read()
#     if ret == True:
#         #just turn the picture
#         #frame = cv2.flip(frame,0)
#
#         #if test acp delet the window here and test again
#         cv2.imshow('frame',frame)
#         key = cv2.waitKey(25)
#         if key == 27:
#             break
#     else:
#         break
# '''
